﻿using _8Group.MES.ISongService.MdItemType;
using _8Group.MES.ISongService.MdWorkshop;
using _8Group.MES.ISongService.MdWorkshop.DTO;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Yitter.IdGenerator;

namespace _8Group.MES.SongService
{
    [ApiExplorerSettings(GroupName = "宋恒漳")]
    public class MdWorkshopService : ApplicationService, IMdWorkshopService
    {
        /// <summary>
        /// 仓储
        /// </summary>
        private readonly IRepository<MdWorkshop, int> _mdWorkshopRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mdWorkshopRepository"></param>
        public MdWorkshopService(IRepository<MdWorkshop, int> mdWorkshopRepository)
        {
            _mdWorkshopRepository = mdWorkshopRepository;
        }

        /// <summary>
        /// 获取 MdWorkshop 列表
        /// </summary>
        /// <param name="workShopDTO"></param>
        /// <returns></returns>
        public async Task<object> GetMdWorkshopList(RequestMdWorkShopDTO workShopDTO)
        {
            var mdWorkshopList = await _mdWorkshopRepository.GetQueryableAsync();
            mdWorkshopList = mdWorkshopList.WhereIf(workShopDTO.WorkshopName != null, x => x.WorkshopName.Contains(workShopDTO.WorkshopName));
            mdWorkshopList = mdWorkshopList.WhereIf(workShopDTO.WorkshopCode != null, x => x.WorkshopCode.ToLower().Contains(workShopDTO.WorkshopCode.ToLower()));
            int count = mdWorkshopList.Count();
            mdWorkshopList = mdWorkshopList.OrderBy(x => x.Id)
                                         .Skip(((workShopDTO.page - 1) ?? 0) * (workShopDTO.limit ?? 10))
                                         .Take(workShopDTO.limit ?? 10);


            var result = new ResponseModel
            {
                code = "",
                count= count,
                data = mdWorkshopList.ToList(),
                msg = "",
            };
            return result;
        }

        /// <summary>
        /// 删除车间管理
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteMdWorkshop(int id)
        {
            var mdWorkshop = await _mdWorkshopRepository.GetAsync(id);
            await _mdWorkshopRepository.DeleteAsync(mdWorkshop, true);
            return true;
        }

        /// <summary>
        /// 更新车间管理
        /// </summary>
        /// <param name="mdWorkshopDTO"></param>
        /// <returns></returns>
        public async Task<bool> UpdataMdWorkshop(UpdMdIWorkshopDTO mdWorkshopDTO)
        {
            mdWorkshopDTO.UpdateBy = "宋恒漳";
            mdWorkshopDTO.UpdateTime = DateTime.Now;
            var mdWorkshop = AutoMapperExt.MapTo<MdWorkshop>(mdWorkshopDTO);
            await _mdWorkshopRepository.UpdateAsync(mdWorkshop);
            return true;
        }

        /// <summary>
        /// 新增车间管理
        /// </summary>
        /// <param name="mdWorkshopDTO"></param>
        /// <returns></returns>
        public async Task<bool> AddMdWorkshop(AddMdWorkShopDTO mdWorkshopDTO)
        {
            //判断是否 存在相同的车间名称
            var mdWorkshop = await _mdWorkshopRepository.FirstOrDefaultAsync(x => x.WorkshopName == mdWorkshopDTO.WorkshopName);
            if (mdWorkshop != null)
            {
                return false;
            }
            mdWorkshopDTO.CreateBy = "宋恒漳";
            mdWorkshopDTO.CreateTime = DateTime.Now;
            mdWorkshop = AutoMapperExt.MapTo<MdWorkshop>(mdWorkshopDTO);
            await _mdWorkshopRepository.InsertAsync(mdWorkshop);
            return true;
        }
    }
}
